<?php
/*  Copyright 2009 Darwin Correspondence Project and Sam Pablo Kuper

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
    
*/
/*
Plugin Name: wpmagickpix
Plugin URI: http://www.sampablokuper.com/projects/magickpix/wpmagickpix/
Description: Allows images to be uploaded, tagged and searched. Development of this plugin was sponsored by the <a href="http://darwinproject.ac.uk">Darwin Correspondence Project</a> and supported by the <a href="http://www.caret.cam.ac.uk">Centre for Applied Research in Educational Technology (CARET)</a> at the <a href="http://www.cam.ac.uk">University of Cambridge</a>. It is licensed under the <a href="http://www.opensource.org/licenses/gpl-3.0.html">GPLv3</a>.
Version: 0.4
Author: Sam Pablo Kuper
Author URI: http://sampablokuper.com
*/

//function __autoload( $class_name ) {
	//require_once strtolower( $class_name ) . '.php';
//}

class Wpmp {

	//$admin_options_name = 'wpmp_admin_options';

	function __construct() { 
		// Ensure jQuery is loaded.
		wp_enqueue_script( 'jquery' );

		// Call function to modify admin menu.
		add_action( 'admin_menu', array( $this, 'menu' ));

		//register_activation_hook(  __FILE__, array( $wpmp, 'activate' ));
	}

	function get_wpmp_root_url() {
		$wpmp_root_url = get_option('siteurl').'/wp-content/plugins/'.dirname( plugin_basename( __FILE__ ));
		return $wpmp_root_url;
	}

	/*
	* Called when wpMagickPix is activated.
	*/
	function activate() {
	}

	/*
	* Called when WordPress's admin menu is loaded.
	*/
	function menu() {
		// Create a new submenu under Media on the admin menu.
		add_submenu_page( 'upload.php', 'Manage/upload media', 'Manage/upload media', 8, 'wpmp', 'show_admin_page' );
		// Call some javascript to hide the default Media submenus and to replace the default Media link with one to the wpmp admin page.
		wp_enqueue_script( 'wpmagickpixjs', call_user_func( array( $this, 'get_wpmp_root_url' ) ) . '/js/wpmagickpix.js' );
		// Redirect the default Media screen to wpMagickPix's media library screen.
		if ( preg_match( '#wp-admin/upload.php$#',$_SERVER['REQUEST_URI'] )) {
			wp_redirect( get_option( 'siteurl' ) . '/wp-admin/upload.php?page=wpmp' ); 
			// For some reason, wp_redirect doesn't seem to work here without a subsequent command like this:
			echo 'makeitwork';
		}
	}

}

// Instantiate the wpMagickPix class.
$wpmp = new Wpmp();

// Finally, show the admin page. WordPress doesn't seem willing to call this
// function if it's kept inside the Wpmp class, which is why it's down here.
function show_admin_page() {
	require_once( 'wpmpadminpage.php' );
	$wpmp_admin_page = new WpmpAdminPage();
}

?>
